Plotly

Sometimes it’s nice (especially for EDA), to have plots that are scrollable and zoomable. The plotly package allows you to do this. Plotly is its own thing https://plotly.com/r/ and is meant for making customized dashboards and interactive web graphics. There’s much, much more it can do. It also works in Python. We don’t really need to learn how plotly works to get a basic plot, because the ggplotly command will can convert a ggplot into a plotly plot.

library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
p1 <- ds %>% ggplot(aes(x = age, y = AUC_dist, color = age_group)) + 
  geom_point() + 
  theme(legend.position = "none") + 
  scale_color_manual(values = custom_palette)
ggplotly(p1)

Animated plots

Another way to extend ggplot is using the gganimate package. By default, the animations render as .gif which makes them embeddable in an html presentation and shareable online. You can get fancy and render them as video files to put in presentations. I’m scratching the surface here, but the easiest thing to do is map a discrete factor to transition_states and you will get an animation that is split by that factor. It’s pretty much like facet_wrap, but uses time instead of space. Putting "{closest_state}" into the title will label the level of the factor in the animation so that you know what you’re looking at.

library(gganimate)

ds %>% ggplot(aes(x = age, y = AUC_dist, color = age_group, group = id)) + 
  geom_point() + 
  transition_states(stim) + 
  ggtitle("{closest_state}") + 
  scale_color_manual(values = custom_palette)